package javachess;

public class Knight extends Piece
{
	public Knight(int a_owner, int a_x, int a_y)
	{
		super ('n', a_owner);
		m_owner = a_owner;
		m_x = a_x;
		m_y = a_y;
	}
	
	/**
	 * -Takes in the x and y destination to determine if the move is acceptable.
	 * -Returns a boolean to indicate 
	 * @param a_x
	 * @param a_y
	 * @return
	 */
	public boolean validateMove(Player player, Player otherPlayer, int a_x, int a_y)
	{
		for(int i = 0; i < player.m_pieces.size(); ++i)
		{
			if(player.m_pieces.get(i).m_x == a_x 
			&& player.m_pieces.get(i).m_y == a_y)
			{
				return false;
			}
		}
		
		if(	(a_x + 1 == m_x && a_y + 2 == m_y)
		||	(a_x + 1 == m_x && a_y - 2 == m_y)
		||	(a_x + 2 == m_x && a_y + 1 == m_y)
		||	(a_x + 2 == m_x && a_y - 1 == m_y)
		||	(a_x - 1 == m_x && a_y + 2 == m_y)
		||	(a_x - 1 == m_x && a_y - 2 == m_y)
		||	(a_x - 2 == m_x && a_y + 1 == m_y)
		||	(a_x - 2 == m_x && a_y - 1 == m_y)
		)
			return true; // ALL POSSIBLE MOVES FOR KNIGHT!
		else
			return false;
		/*
		
		//these must remain in this scope in order to be reset if a swap occurred
		int temp_x = m_x, temp_y = m_y;
		//if the destination x is less than the object x, swap them
		if(a_x < m_x)
		{
			m_x = a_x;
			a_x = temp_x;
		}
		//if the destination y is less than the object y, swap them
		if(a_y < m_y)
		{
			m_y = a_y;
			a_y = temp_y;
		}
		//determine if the knight moved in an L shape
		if((a_x - m_x) == 3 && (a_y - m_y) == 1)
		{
			//in case a swap occurred
			m_x = temp_x;
			m_y = temp_y;
			for(int i = 0; i < player.m_pieces.size(); ++i)
			{
				//if the location is equal to one of the player's own pieces
				if(player.m_pieces.get(i).m_x == a_x && player.m_pieces.get(i).m_y == a_y )
					return false;
			}
			for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
			{
				//if the location is equal to another player's piece
				if(otherPlayer.m_pieces.get(i).m_x == a_x && otherPlayer.m_pieces.get(i).m_y == a_y )
					return true;
			}
			return true;
		}
		else if((a_x - m_x) == 1 && (a_y - m_y) == 3)
		{
			//in case a swap occurred
			m_x = temp_x;
			m_y = temp_y;
			for(int i = 0; i < player.m_pieces.size(); ++i)
			{
				//if the location is equal to one of the player's own pieces
				if(player.m_pieces.get(i).m_x == a_x && player.m_pieces.get(i).m_y == a_y )
					return false;
			}
			for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
			{
				//if the location is equal to another player's piece
				if(otherPlayer.m_pieces.get(i).m_x == a_x && otherPlayer.m_pieces.get(i).m_y == a_y )
					return true;
			}
			return true;
		}
		//in case a swap occurred
		m_x = temp_x;
		m_y = temp_y;
		return false;
		*/
	}
}
